home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / 3dprograms / rayshade-4.0 / fixes / fix023 < prev    next >
Internet Message Format  |  1995-02-13  |  15KB

  1. From theseas!Princeton.EDU!cek Tue, 28 Jun 94 16:52:03 EET
  2. Received: by kriton.UUCP (V1.16/Amiga)
  3.     id AA00000; Tue, 28 Jun 94 16:52:03 EET
  4. Received: by theseas.ntua.gr with UUCP; Tue, 28 Jun 1994 15:30:38 +0300
  5. Received: from nemesis.ics.forth.gr (nemesis.csi.forth.gr) by pythia.ics.forth.gr via FORTHnet with SMTP;
  6.     id AA25343 (5.65c/FORTH-ICS-3.0-MHS-7.0); Tue, 28 Jun 1994 14:55:29 +0300
  7. Received: from eunet.EU.net by nemesis.ics.forth.gr via FORTHnet with SMTP;
  8.     id AA10498 (5.65c/ICS-1.1); Tue, 28 Jun 1994 14:47:15 +0300 (EET DST)
  9. Received: (uunet@localhost) by eunet.EU.net (8.6.8/8.6.4) with UUCP id NAA06872 for ariadne!theseas!kriton!kyrimis; Tue, 28 Jun 1994 13:51:15 +0200
  10. Received: from Princeton.EDU by relay2.UU.NET with SMTP 
  11.     (rama) id QQwwfv14573; Tue, 28 Jun 1994 07:50:50 -0400
  12. Received: from fs.Princeton.EDU by Princeton.EDU (5.65b/2.111/princeton)
  13.     id AA04294; Tue, 28 Jun 94 07:46:16 -0400
  14. Received: by cs.Princeton.EDU (4.1/1.105)
  15.     id AA01584; Tue, 28 Jun 94 07:46:15 EDT
  16. Received: from arthur.cs.purdue.edu by cs.Princeton.EDU (4.1/1.105)
  17.     id AA01530; Tue, 28 Jun 94 07:46:01 EDT
  18. Received: from icarus.cs.purdue.edu (root@icarus.cs.purdue.edu [128.10.8.56])
  19.     by arthur.cs.purdue.edu (8.6.4/PURDUE_CS-1.3) with ESMTP
  20.     id <GAA19676> for <rayshade-users@cs.princeton.edu>; Tue, 28 Jun 1994 06:45:58 -0500
  21. Received: from localhost (bj@localhost [127.0.0.1])
  22.     by icarus.cs.purdue.edu (8.6.4/PURDUE_CS-1.3) with SMTP
  23.     id <GAA02160> for <rayshade-users@cs.princeton.edu>; Tue, 28 Jun 1994 06:45:57 -0500
  24. Organization: Institute of Computer Science, FORTH, P.O.Box 1385,
  25.      Heraklio, Crete, GR-711-10, Greece
  26.     tel.: +30 (81) 39 16 00,  fax: +30 (81) 39 16 01
  27. Message-Id: <199406281145.GAA02160@icarus.cs.purdue.edu>
  28. Date: Tue, 28 Jun 1994 06:45:57 -0500
  29. Errors-To: rayshade-users-request@cs.Princeton.EDU
  30. Remailed-Date: Tue Jun 28 07:46:03 EDT 1994
  31. From: bj@cs.purdue.edu (Ben Jackson)
  32. To: rayshade-users@cs.Princeton.EDU (Rayshade List)
  33. Subject: color raypaint under X
  34.  
  35. When I first built rayshade, I just needed it as a backend, so I didn't
  36. try to built raypaint (I assumed it needed the GL library).  Since then
  37. I've become a rayshade addict, and I built raypaint last night.  I was
  38. disappointed to discover that it would only display grayscale images,
  39. so I decided to fix it.  :-)
  40.  
  41. I figured that the original author had punted on the 8bit color code
  42. because of palette computations, so I just used a 6x6x6 standard
  43. colormap (much like xv's 24 bit mode).  At first I thought I was clever
  44. and made an adaptive algorithm that took advantage of the fact that
  45. blue and red aren't as "important" as green.  It generated 6x7x5 color
  46. maps as a first try.  It took me a LONG time to figure out what that
  47. doesn't work.
  48.  
  49. Like the grayscale code, it first tries to share the map, and backs
  50. down (5x5x5 etc down to 2x2x2) if it can't get enough colors.  If it
  51. can't get a 2x2x2 set of colors in the default map, it starts over with
  52. a private colormap.  If it fails again, it falls back on the the
  53. grayscale code (what the heck).  Everyone I know with a 24bit
  54. framebuffer also has the GL library, so I didn't worry about taking
  55. advantage of 24bit Visuals (which should probably use DirectColor
  56. anyway).
  57.  
  58. A 2x2x2 map is only 8 colors (duh), and the grayscale code insists on
  59. at least 16 colors.  2x2x2 might be too small, but I decided I'd let
  60. you guys decide if 3x3x3 or higher would be a more reasonable limit.
  61.  
  62. Since grayscale is useful on 8bit displays (giving more detail in some
  63. cases than a 6x6x6 colormap), I decided to add an option to force the
  64. gray map.  This wasn't as straightforward as I'd hoped, since raypaint
  65. uses the same option parser as rayshade, which doesn't need this option
  66. (and even if it did, you might want to watch the rendering in grayscale
  67. while still generating 24bit color output).  I added a small check for
  68. '-M' in raypaint/main.c.  If it sees it, it sets the flag and removes
  69. it from the options list.  This is a hack, but I don't see a better way
  70. to do it without also adding the option to rayshade.
  71.  
  72. While I was in the code, I fixed a bug in the Rectangle rendering
  73. code.  It was approximating the color of the whole rectangle as the
  74. color of the lower left corner.  I added the code to average the four
  75. color values, which makes the scene look a bit more realistic while
  76. it's still blocky (which makes it easier to find the regions you want
  77. to refine).  I did manage to resist the temptation to run the whole file
  78. through GNU Indent, since that would make the diff's unreasonable.
  79.  
  80. Once again, I'm not on the rayshade-users list, so please reply to (or
  81. at least Cc) me.  Comments welcome.
  82.  
  83. --Ben
  84.  
  85. *** raypaint/main.c.orig    Tue Jun 28 05:19:44 1994
  86. --- raypaint/main.c    Tue Jun 28 05:48:15 1994
  87. ***************
  88. *** 31,36 ****
  89. --- 31,37 ----
  90.   #include "picture.h"
  91.   
  92.   void RSInitialize(), RSStartFrame();
  93. + int gray;
  94.   
  95.   int
  96.   main(argc, argv)
  97. ***************
  98. *** 65,71 ****
  99.       /*
  100.        * Render the image.
  101.        */
  102. !     Render(argc, argv);
  103.       StatsPrint();
  104.       return 0;
  105.   }
  106. --- 66,72 ----
  107.       /*
  108.        * Render the image.
  109.        */
  110. !     Render(argc, argv, gray);
  111.       StatsPrint();
  112.       return 0;
  113.   }
  114. ***************
  115. *** 100,109 ****
  116. --- 101,126 ----
  117.   int argc;
  118.   char **argv;
  119.   {
  120. +     int i;
  121.       /*
  122.         * Initialize variables, etc.
  123.        */
  124.       RSSetup();
  125. +     /*
  126. +      * Steal the gray option, -M (mono, OK?)
  127. +      */
  128. +     gray = 0;
  129. +     for (i = 1; i < argc; ++i) {
  130. +         if ('-' != argv[i][0] || 'M' != argv[i][1]) {
  131. +             continue;
  132. +         }
  133. +         gray = 1;
  134. +         for (; i < argc; ++i) {
  135. +             argv[i] = argv[i+1];
  136. +         }
  137. +         --argc;
  138. +     }
  139.       /*
  140.        * Parse options from command line.
  141.        */
  142. *** raypaint/render.c.orig    Tue Jun 28 05:44:07 1994
  143. --- raypaint/render.c    Tue Jun 28 05:46:07 1994
  144. ***************
  145. *** 122,130 ****
  146.   int    SuperSampleMode = 0;
  147.   #define SSCLOSED (SuperSampleMode + 1)
  148.   
  149. ! Render(argc, argv)
  150.   int argc;
  151.   char **argv;
  152.   {
  153.       /*
  154.        * Do an adaptive trace, displaying results in a
  155. --- 122,131 ----
  156.   int    SuperSampleMode = 0;
  157.   #define SSCLOSED (SuperSampleMode + 1)
  158.   
  159. ! Render(argc, argv, gray)
  160.   int argc;
  161.   char **argv;
  162. + int gray;
  163.   {
  164.       /*
  165.        * Do an adaptive trace, displaying results in a
  166. ***************
  167. *** 148,154 ****
  168.        */
  169.       TopRay.time = Options.framestart;
  170.   
  171. !     GraphicsInit(Screen.xsize, Screen.ysize, "rayview");
  172.       /*
  173.        * Allocate array of samples.
  174.        */
  175. --- 149,155 ----
  176.        */
  177.       TopRay.time = Options.framestart;
  178.   
  179. !     GraphicsInit(Screen.xsize, Screen.ysize, "rayview", gray);
  180.       /*
  181.        * Allocate array of samples.
  182.        */
  183. *** raypaint/xgraphics.c.orig    Sun Feb  9 22:03:51 1992
  184. --- raypaint/xgraphics.c    Tue Jun 28 05:45:59 1994
  185. ***************
  186. *** 33,40 ****
  187.   GC gc;
  188.   Window win;
  189.   int screen_height;
  190.   
  191. ! unsigned long graymap[256];
  192.   int max_colors;
  193.   double one_over_gamma = 0.4;
  194.   
  195. --- 33,41 ----
  196.   GC gc;
  197.   Window win;
  198.   int screen_height;
  199. + int gray;
  200.   
  201. ! unsigned long colormap[256];
  202.   int max_colors;
  203.   double one_over_gamma = 0.4;
  204.   
  205. ***************
  206. *** 42,47 ****
  207. --- 43,49 ----
  208.    * Sets the gray color map for the device.  A 2.5 gamma map is used
  209.    * by default.
  210.    */
  211. + static
  212.   setup_gray_gamma_map()
  213.   {
  214.       int cc, col;
  215. ***************
  216. *** 49,54 ****
  217. --- 51,57 ----
  218.   
  219.       XColor xcolor;
  220.   
  221. +     gray = 1;
  222.       /* Use the default colormap if possible. */
  223.       if ( vis == DefaultVisualOfScreen( scrn ) )
  224.           cmap = DefaultColormapOfScreen( scrn );
  225. ***************
  226. *** 67,77 ****
  227.               xcolor.blue= gamma_color;
  228.               if (!XAllocColor(dpy, cmap, &xcolor)) {
  229.                   for (cc=0; cc < col; cc++)
  230. !                     XFreeColors(dpy, cmap, &graymap[cc], 1, 0);
  231.                   col = 0;
  232.                   break;
  233.               }
  234. !             graymap[col] = xcolor.pixel;
  235.           }
  236.           if (col)
  237.               return;
  238. --- 70,80 ----
  239.               xcolor.blue= gamma_color;
  240.               if (!XAllocColor(dpy, cmap, &xcolor)) {
  241.                   for (cc=0; cc < col; cc++)
  242. !                     XFreeColors(dpy, cmap, &colormap[cc], 1, 0);
  243.                   col = 0;
  244.                   break;
  245.               }
  246. !             colormap[col] = xcolor.pixel;
  247.           }
  248.           if (col)
  249.               return;
  250. ***************
  251. *** 85,97 ****
  252.           exit(-2);
  253.       }
  254.       for(cc=0; cc < 256; cc++)
  255. !         if (!XAllocColorCells(dpy, cmap, False, NULL, 0, &graymap[cc], 1))
  256.               break;
  257.       max_colors = cc;
  258.   
  259.       xcolor.flags= DoRed | DoGreen | DoBlue;
  260.       for(col=0; col < max_colors; col++) {
  261. !         xcolor.pixel= graymap[col];
  262.           gamma_color = (pow((float) col / (float) max_colors,
  263.                              one_over_gamma) * 65536);
  264.           xcolor.red= gamma_color;
  265. --- 88,100 ----
  266.           exit(-2);
  267.       }
  268.       for(cc=0; cc < 256; cc++)
  269. !         if (!XAllocColorCells(dpy, cmap, False, NULL, 0, &colormap[cc], 1))
  270.               break;
  271.       max_colors = cc;
  272.   
  273.       xcolor.flags= DoRed | DoGreen | DoBlue;
  274.       for(col=0; col < max_colors; col++) {
  275. !         xcolor.pixel= colormap[col];
  276.           gamma_color = (pow((float) col / (float) max_colors,
  277.                              one_over_gamma) * 65536);
  278.           xcolor.red= gamma_color;
  279. ***************
  280. *** 101,109 ****
  281.       }
  282.   }
  283.   
  284. ! GraphicsInit(xsize, ysize, name)
  285.   int xsize, ysize;
  286.   char *name;
  287.   {
  288.       int win_size;
  289.       XSetWindowAttributes attrs;
  290. --- 104,181 ----
  291.       }
  292.   }
  293.   
  294. ! /*****************************************************************
  295. !  * Sets the color map for the device.  A 2.5 gamma map is used
  296. !  * by default, with a direct 6x7x5 colormap
  297. !  */
  298. ! static
  299. ! setup_color_gamma_map()
  300. ! {
  301. !     static int first_try = 1;
  302. !     int cc, col;
  303. !     int r, g, b;
  304. !     int done;
  305. !     XColor xcolor;
  306. !     /* Use the default colormap if possible. */
  307. !     if (first_try && vis == DefaultVisualOfScreen(scrn)) {
  308. !         cmap = DefaultColormapOfScreen(scrn);
  309. !     } else {
  310. !         cmap = XCreateColormap(dpy, RootWindowOfScreen(scrn),
  311. !                                vis, AllocNone );
  312. !     }
  313. !     /* try to share with current colormap */
  314. !     xcolor.flags= DoRed | DoGreen | DoBlue;
  315. !     for (max_colors = 6; max_colors >= 2; --max_colors) {
  316. !     done = 1; /* gets unset on failure */
  317. !     col = 0;
  318. !     for (r = 0; r < max_colors; ++r) {
  319. !         xcolor.red = pow((float) r / (float) max_colors,
  320. !         one_over_gamma) * 65536;
  321. !         for (g = 0; g < max_colors; ++g) {
  322. !         xcolor.green = pow((float) g / (float) max_colors,
  323. !             one_over_gamma) * 65536;
  324. !         for (b = 0; b < max_colors; ++b) {
  325. !             xcolor.blue = pow((float) b / (float) max_colors,
  326. !                     one_over_gamma) * 65536;
  327. !             if (!XAllocColor(dpy, cmap, &xcolor)) {
  328. !             for (cc=0; cc < col; cc++)
  329. !                 XFreeColors(dpy, cmap, &colormap[cc], 1, 0);
  330. !             r = g = b = 99;
  331. !                 done = 0;
  332. !                 break;
  333. !             }
  334. !             colormap[col++] = xcolor.pixel;
  335. !         }
  336. !         }
  337. !     }
  338. !     if (done)
  339. !         break;
  340. !     }
  341. !     if (!done) {
  342. !     if (first_try) {
  343. !         /* go back and try with a private color map */
  344. !         first_try = 0;
  345. !         setup_color_gamma_map();
  346. !     }
  347. !     /* our smallest color set (2x2x2 = 8) is smaller than the
  348. !      * grayscale code (16), but we still try the gray map because
  349. !      * it might be able to share colors.
  350. !      */
  351. !         fprintf(stderr, "Could not create color map -- trying grayscale\n");
  352. !     setup_gray_gamma_map();
  353. !     return;
  354. !     }
  355. !     fprintf(stderr, "using standard %dx%dx%d colormap\n", max_colors, max_colors, max_colors);
  356. !     gray = 0;
  357. ! }
  358. ! GraphicsInit(xsize, ysize, name, gray)
  359.   int xsize, ysize;
  360.   char *name;
  361. + int gray;
  362.   {
  363.       int win_size;
  364.       XSetWindowAttributes attrs;
  365. ***************
  366. *** 143,149 ****
  367.           scrn = ScreenOfDisplay( dpy, max_vis->screen );
  368.           gc = DefaultGCOfScreen( scrn );
  369.   
  370. !         setup_gray_gamma_map();
  371.   
  372.           XFree( (char *)vis_list );
  373.       }
  374. --- 215,225 ----
  375.           scrn = ScreenOfDisplay( dpy, max_vis->screen );
  376.           gc = DefaultGCOfScreen( scrn );
  377.   
  378. !     if (gray) {
  379. !         setup_gray_gamma_map();
  380. !     } else {
  381. !             setup_color_gamma_map();
  382. !     }
  383.   
  384.           XFree( (char *)vis_list );
  385.       }
  386. ***************
  387. *** 181,192 ****
  388.   int xp, yp;
  389.   unsigned char color[3];
  390.   {
  391. -     float bwvalue;
  392.       int val;
  393.   
  394. !     bwvalue = ( 0.35*color[0] + 0.55*color[1] + 0.10*color[2] ) / 256.0;
  395. !     val = (int) ( bwvalue * max_colors );
  396. !     XSetForeground( dpy, gc, graymap[val] );
  397.       XFillRectangle( dpy, win, gc, xp, (screen_height - (yp + 1)),
  398.                       1, 1 );
  399.   }
  400. --- 257,274 ----
  401.   int xp, yp;
  402.   unsigned char color[3];
  403.   {
  404.       int val;
  405.   
  406. !     if (gray) {
  407. !     val = (0.35*color[0] + 0.55*color[1] + 0.10*color[2]) / 256.0 * max_colors;
  408. !     } else {
  409. !     val = color[0] / 256.0 * max_colors;
  410. !     val *= max_colors;
  411. !     val += color[1] / 256.0 * max_colors;
  412. !     val *= max_colors;
  413. !     val += color[2] / 256.0 * max_colors;
  414. !     }
  415. !     XSetForeground( dpy, gc, colormap[val] );
  416.       XFillRectangle( dpy, win, gc, xp, (screen_height - (yp + 1)),
  417.                       1, 1 );
  418.   }
  419. ***************
  420. *** 200,211 ****
  421.   int xp, yp, xs, ys;
  422.   unsigned char ll[3], lr[3], ur[3], ul[3];
  423.   {
  424. -     float bwvalue;
  425.       int val;
  426.   
  427. !     bwvalue = ( 0.35*ll[0] + 0.55*ll[1] + 0.10*ll[2] ) / 256.0;
  428. !     val = (int) ( bwvalue * max_colors );
  429. !     XSetForeground( dpy, gc, graymap[val] );
  430.       XFillRectangle( dpy, win, gc, xp, (screen_height - (yp + ys + 1)),
  431.                       xs+1, ys+1 );
  432.       XFlush( dpy );
  433. --- 282,302 ----
  434.   int xp, yp, xs, ys;
  435.   unsigned char ll[3], lr[3], ur[3], ul[3];
  436.   {
  437.       int val;
  438.   
  439. !     ll[0] = (ll[0] + lr[0] + ur[0] + ul[0]) / 4;
  440. !     ll[1] = (ll[1] + lr[1] + ur[1] + ul[1]) / 4;
  441. !     ll[2] = (ll[2] + lr[2] + ur[2] + ul[2]) / 4;
  442. !     if (gray) {
  443. !     val = (0.35*ll[0] + 0.55*ll[1] + 0.10*ll[2]) / 256.0 * max_colors;
  444. !     } else {
  445. !     val = ll[0] / 256.0 * max_colors;
  446. !     val *= max_colors;
  447. !     val += ll[1] / 256.0 * max_colors;
  448. !     val *= max_colors;
  449. !     val += ll[2] / 256.0 * max_colors;
  450. !     }
  451. !     XSetForeground( dpy, gc, colormap[val] );
  452.       XFillRectangle( dpy, win, gc, xp, (screen_height - (yp + ys + 1)),
  453.                       xs+1, ys+1 );
  454.       XFlush( dpy );
  455. ***************
  456. *** 275,281 ****
  457.   {
  458.           XEvent event;
  459.           if (XCheckTypedEvent(dpy, Expose, &event)) {
  460. !                 XSetForeground( dpy, gc, graymap[0] );
  461.                   XFillRectangle( dpy, win, gc, event.xexpose.x, event.xexpose.y,
  462.                       event.xexpose.width, event.xexpose.height );
  463.                   XFlush( dpy );
  464. --- 366,372 ----
  465.   {
  466.           XEvent event;
  467.           if (XCheckTypedEvent(dpy, Expose, &event)) {
  468. !                 XSetForeground( dpy, gc, colormap[0] );
  469.                   XFillRectangle( dpy, win, gc, event.xexpose.x, event.xexpose.y,
  470.                       event.xexpose.width, event.xexpose.height );
  471.                   XFlush( dpy );
  472.  
  473. ----------
  474. Administrivia: rayshade-users-request@cs.princeton.edu
  475. Mailing list: rayshade-users@cs.princeton.edu
  476.  
  477.